package com.jia;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("name","jiajia");
        jsonObject.put("age","18");
        String res = jsonObject.toJSONString();
        // 开启事物
        Transaction multi = jedis.multi();

        try {
            multi.set("user1",res);
            multi.set("user2",res);
            int a =  1 / 0;
            multi.exec();
        } catch (Exception e) {
            multi.discard(); // 放弃事物
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }

    }
}